package cn.map.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author 後溪
 */
public class DateUtil {

    private static  final SimpleDateFormat SIMPLE_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private static  final SimpleDateFormat SIMPLE_DATE_FORMAT_BY_OSS = new SimpleDateFormat("yyyyMMdd");
    private static  final SimpleDateFormat SIMPLE_DATE_FORMAT_BY_AGENT_SYNC = new SimpleDateFormat("yyyy-MM");
    private static  final SimpleDateFormat SIMPLE_DATE_FORMAT_BY_MM_DD = new SimpleDateFormat("MM-dd");

    public static Date toFormate(Date date) throws ParseException {
        String format = SIMPLE_DATE_FORMAT.format(date);
        return SIMPLE_DATE_FORMAT.parse(format);
    }


    public static String toFormateByMM_dd(String date) throws ParseException {
        Date format = SIMPLE_DATE_FORMAT_BY_MM_DD.parse(date);
//        return SIMPLE_DATE_FORMAT_BY_MM_DD.parse(format);
        return SIMPLE_DATE_FORMAT_BY_MM_DD.format(format);
    }

    public static String toFormateByOss(Date date) throws ParseException {
        return SIMPLE_DATE_FORMAT_BY_OSS.format(date);
    }

    public static String toFormateByAgentSync(Date date) throws ParseException {
        return SIMPLE_DATE_FORMAT_BY_AGENT_SYNC.format(date);
    }

    public static long getDifferDays(Date date) throws ParseException {
        SimpleDateFormat simpleDateFormatByYyyyMmDd = new SimpleDateFormat("yyyy-MM-dd");
        Date oldDate = simpleDateFormatByYyyyMmDd.parse(simpleDateFormatByYyyyMmDd.format(date));
        Date nowDate = new Date();
        long oldDateTime = oldDate.getTime();
        long nowDateTime = nowDate.getTime();
        return (nowDateTime-oldDateTime)/1000/60/60/24;
    }

    /**
     * 根据时间戳与随机数随机生成券编号
     * @return 券编号
     */
    public synchronized static String randomCouponNumber(){
        String couponNumber = "YQ"+System.currentTimeMillis();
        int randomCode = (int)(Math.random()*(999999-100000+1))+100000;
        return couponNumber+randomCode;
    }
}
